<template>
    <el-drawer v-model="showDrawer" :title="title" :size="size" :close-on-click-modal="false" :destroy-on-close="destroyOnClose">
        <div class="formDrawer">
            <div class="body">
                <slot></slot>
            </div>
            <div class="actions">
                <el-button type="primary" :loading="loading" @click="submit">
                    {{ confirmText }}
                </el-button>
                <el-button type="default" @click="close">取消</el-button>
            </div>
        </div>
    </el-drawer>
</template>
<script setup>
import { ref } from 'vue';
const showDrawer = ref(false);
const open = () => (showDrawer.value = true);
const close = () => (showDrawer.value = false);

// 接收父传子的自定义事件
const emit = defineEmits(['submit']);
// 触发自定义事件
// 这里相当于调用了this.$emit
const submit = () => emit('submit');

const loading = ref(false);
const showLoading = () => (loading.value = true);
const hideLoading = () => (loading.value = false);

// 接收父传子的值
const props = defineProps({
    title: String,
    size: {
        type: String,
        default: '45%',
    },
    destroyOnClose: {
        type: Boolean,
        default: false,
    },
    confirmText: {
        type: String,
        default: '提交',
    },
});

// 向父组件暴露一下方法,父组件可以通过ref获取节点调用子类的方法,实现子组件的显示与隐藏
defineExpose({
    open,
    close,
    showLoading,
    hideLoading,
});
</script>

<style>
.formDrawer {
    width: 100%;
    height: 100%;
    position: relative;
    @apply flex;
}

.formDrawer .body {
    flex: 1;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 50px;
    overflow-y: auto;
}

.formDrawer .actions {
    height: 50px;
    @apply mt-auto flex items-center;
}
</style>
